import { MockMethod } from 'vite-plugin-mock'

let routes = [
  {
    id: 1,
    path: '/product/list',
    name: 'ProductList',
    component: 'views/product/list',
    title: '商品列表',
    icon: 'Goods',
    sort: 1,
    createTime: '2024-01-01 12:00:00'
  },
  {
    id: 2,
    path: '/user',
    name: 'User',
    component: 'views/user/index',
    title: '用户管理',
    icon: 'User',
    sort: 2,
    createTime: '2024-01-01 12:00:00'
  }
]

export default [
  {
    url: '/api/routes',
    method: 'get',
    response: () => ({
      code: 200,
      data: routes,
      message: 'success'
    })
  },
  {
    url: '/api/route',
    method: 'post',
    response: ({ body }) => {
      const newRoute = {
        ...body,
        id: routes.length + 1,
        createTime: new Date().toLocaleString()
      }
      routes.push(newRoute)
      return {
        code: 200,
        data: newRoute,
        message: 'success'
      }
    }
  },
  {
    url: '/api/route/:id',
    method: 'put',
    response: ({ body }) => {
      const index = routes.findIndex(item => item.id === body.id)
      if (index > -1) {
        routes[index] = { ...routes[index], ...body }
        return {
          code: 200,
          data: routes[index],
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '路由不存在'
      }
    }
  },
  {
    url: '/api/route/:id',
    method: 'delete',
    response: ({ params }) => {
      const index = routes.findIndex(item => item.id === Number(params.id))
      if (index > -1) {
        routes.splice(index, 1)
        return {
          code: 200,
          message: 'success'
        }
      }
      return {
        code: 400,
        message: '路由不存在'
      }
    }
  }
] as MockMethod[] 